﻿
var ns, toast, actionsheet;
var app = new Vue({
    el: '#app',
    data: {
        postStr: '',
        form: {
            ID: 0,
            roomId: 0,
            chargeId: 0,
            feeStartTime: '',
            feeEndTime: '',
            useCount: 0,
            unitPrice: '',
            totalCost: '',
            discountId: 0,
            discountType: 0,
            discountName: '',
            discountCost: 0,
            breakTotalCost: 0,
            approveUserId: 0,
            approveUserName: '',
            remark: '',
            minDiscountCost: 0,
            maxDiscountCost: 0,
            canWriteDiscount: false,
            isApprove: false
        },
        discountList: [],
        discountUserList: [],
        submiting: false,
        discountId: 0,
        discountCost: 0,
    },
    methods: {
        get_data: function () {
            var that = this;
            ns.post({
                action: 'getchargediscountcombobox',
                chargeId: that.form.chargeId,
                feeStartTime: that.form.feeStartTime,
                feeEndTime: that.form.feeEndTime,
                feeTotalCost: that.form.feeTotalCost
            }, function (succeed, data, err) {
                if (succeed) {
                    that.discountList = data.list;
                    that.do_selectDiscount();
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        do_selectDiscount: function () {
            var that = this;
            if (that.form.discountId <= 0) {
                that.form.discountName = '';
                that.form.discountType = '';
                that.form.discountCost = '';
                that.form.minDiscountCost = 0;
                that.form.maxDiscountCost = 0;
                that.form.canWriteDiscount = false;
                return;
            }
            ns.post({
                action: 'calculatediscountcostbymodel',
                ID: that.form.ID,
                discountId: that.form.discountId,
                feeStartTime: that.form.feeStartTime,
                feeEndTime: that.form.feeEndTime,
                totalCost: that.form.totalCost,
                useCount: that.form.useCount,
                chargeId: that.form.chargeId,
                roomId: that.form.roomId,
                unitPrice: that.form.unitPrice
            }, function (succeed, data, err) {
                if (succeed) {
                    that.form.isApprove = data.response.isApprove;
                    that.form.minDiscountCost = data.response.MinDiscountCost;
                    that.form.maxDiscountCost = (data.response.MaxDiscountCost > that.form.totalCost || data.response.MaxDiscountCost == 0) ? that.form.totalCost : data.response.MaxDiscountCost;
                    if (!data.response.isManualWrite) {
                        that.form.canWriteDiscount = false;
                        that.form.discountCost = data.response.AppDiscountCost
                    }
                    else {
                        that.form.canWriteDiscount = true;
                    }
                    var myDiscount = that.discountList.find(x => x.ID == that.form.discountId)
                    if (myDiscount && myDiscount.DiscountType == 'averagesharing') {
                        that.form.canWriteDiscount = true;
                    }
                }
                that.discountUserList = [];
                if (that.form.isApprove) {
                    ns.post({
                        action: 'getdiscountapproveuserlist',
                        discountId: that.form.discountId,
                    }, function (succeed, data, err) {
                        if (succeed) {
                            that.discountUserList = data.discountUserList;
                        }
                    });
                }
            });
        },
        do_selectUser: function () {
            var that = this;
        },
        do_save: function (item) {
            var that = this;
            //能填写时 判断填写金额需大于0
            if (that.form.discountCost <= 0 && that.form.canWriteDiscount) {
                api.toast({
                    msg: '优惠金额必须大于0',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.form.discountCost == 0 && that.form.isApprove) {
                api.toast({
                    msg: '优惠金额必须大于0',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.form.maxDiscountCost > 0 && that.form.discountCost > that.form.maxDiscountCost) {
                api.toast({
                    msg: '优惠金额不能大于' + that.form.maxDiscountCost,
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (!that.form.approveUserId && that.form.isApprove) {
                api.toast({
                    msg: '请选择审批人',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (!that.form.isApprove) {
                api.sendEvent({
                    name: 'submitdiscountsuccess',
                    extra: {
                        discountId: that.form.discountId,
                        cost: that.form.discountCost,
                        feeId: that.form.ID,
                        stateId: 0,
                        stateDesc: '',
                    }
                });
                setTimeout(() => {
                    api.closeWin();
                }, 1000);
                return;
            }
            that.submiting = true;
            ns.post({
                action: 'saveroomfeediscountapplicant',
                feeId: that.form.ID,
                discountId: that.form.discountId,
                discountCost: that.form.discountCost,
                approveUserId: that.form.approveUserId,
                approveRemark: that.form.remark,
                feeStartTime: that.form.feeStartTime,
                feeEndTime: that.form.feeEndTime,
                totalCost: that.form.totalCost,
                useCount: that.form.useCount,
                chargeId: that.form.chargeId,
                roomId: that.form.roomId,
                unitPrice: that.form.unitPrice,
                breakTotalCost: that.form.breakTotalCost
            }, function (succeed, data, err) {
                that.submiting = false;
                if (succeed) {
                    api.toast({
                        msg: '优惠申请提交成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    api.sendEvent({
                        name: 'submitdiscountsuccess',
                        extra: {
                            discountId: that.form.discountId,
                            cost: that.form.discountCost,
                            feeId: data.feeId,
                            stateId: 8,
                            stateDesc: '审批中',
                        }
                    });
                    setTimeout(() => {
                        api.closeWin();
                    }, 1000);
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        }
    }
});

apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    app.form.ID = api.pageParam.id || '';
    app.form.chargeId = api.pageParam.chargeId || '';
    app.form.feeStartTime = api.pageParam.feeStartTime || '';
    app.form.feeEndTime = api.pageParam.feeEndTime || '';
    app.form.totalCost = api.pageParam.totalCost || '';
    app.form.useCount = api.pageParam.useCount || '';
    app.form.roomId = api.pageParam.roomId || '';
    app.form.unitPrice = api.pageParam.unitPrice || '';
    app.form.breakTotalCost = api.pageParam.breakTotalCost || '';
    app.form.discountId = api.pageParam.discountId || 0;
    app.form.discountCost = api.pageParam.discountCost || 0;
    app.get_data();
};
